<template>
  <div class="login_contailer">
    <div class="login_box">
      <!--      头像区-->
      <div class="avatar_box">
        <img src="../assets/sunpine.jpg" alt=""/>
      </div>
      <!--      表单区-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormrules" label-width="0px" class="login_form">
        <el-form-item prop="loginName">
          <el-input v-model="loginForm.loginName" prefix-icon="iconfont icon-yonghu" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="loginPassword">
          <el-input v-model="loginForm.loginPassword" type="password" prefix-icon="iconfont icon-suo"
                    placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    loginName: 'admin',
                    loginPassword: '123'
                },
                loginFormrules: {
                    loginName: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在3位10字符之间', trigger: 'blur'}
                    ], loginPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 3, max: 15, message: '长度在3位15字符之间', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            resetLoginForm() {
                // console.log(this)
                this.$refs.loginFormRef.resetFields();
            },
            login() {
                this.$refs.loginFormRef.validate(valid => {
                    console.log(valid)
                    if (!valid) {
                        return;
                    }
                    this.$http({
                        url:'login',
                        method:'post',
                        data:this.loginForm
                    }).then(res=>{
                        console.log(res);
                        if (res.code !== 200) {
                            console.log("登录失败");
                            return this.$message.error({
                                showClose: true,
                                message: '登录失败。'
                            });
                        }
                        this.$message.success({
                            showClose: true,
                            message: '登录成功。'
                        });
                        window.sessionStorage.setItem("token", res.token);
                        this.$router.push("/home");
                    }).catch(err=>{
                        console.log("登录失败");
                        return this.$message.error({
                            showClose: true,
                            message: '登录失败。'
                        });
                    })
                });
            }
        }
    }
</script>

<style lang="less" scoped>
  .login_contailer {
    background: #eee;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      }
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
</style>